package com.luoqy.speedy.common;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import com.luoqy.speedy.core.cache.CacheServiceImpl;

/**
 * @author luoqy
 * @date 2019年6月2日 配置文件操作工具
 */
public class ConfigManage {
	public static Map<String, String> dataConfig=dataConfigs();
	
	public static Map<String, String> dataConfigs(){
		CacheServiceImpl cache=new CacheServiceImpl();
		Map<String, String> data=ConfigManage.findProperties("jdbc");
		return data;
	}
	/**
	 * @param name 配置名称
	 * @return
	 */
	public static Map<String, String> findProperties(String name){
		Map<String, String> map = new HashMap<>();
		try {
			Properties prop = new Properties();
			String filePath = System.getProperty("user.dir")+ "/config/"+name+".properties";
			File file=new File(filePath);
//			System.err.println(file.exists());
			if(!file.exists()){
				System.err.println("没有文件存在");
				file.createNewFile();
			}
	       InputStream in = new BufferedInputStream(new FileInputStream(filePath));
			prop.load(in);
			Enumeration<?> fileName = prop.propertyNames();
			while (fileName.hasMoreElements()) {
				String strKey = (String) fileName.nextElement();
				String strValue = prop.getProperty(strKey);
				map.put(strKey, strValue);
			}
			return map;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
    /**
     * 本地动态修改配置文件数据,
     */
    public static String updateProperties(String fileName, Map<String, String> map) {
        Properties props = new Properties();
        InputStream in=null;
        String path="";
        try {
        	path = System.getProperty("user.dir")+ "/config/"+fileName+".properties";
        	in = new BufferedInputStream(new FileInputStream(path));
            props.load(in);
            Iterator<Map.Entry<String, String>> entries = map.entrySet().iterator();
            while (entries.hasNext()) {
                Map.Entry<String, String> entry = entries.next();
                props.setProperty(entry.getKey(), entry.getValue());
            }
            FileWriter fw = new FileWriter(path);
            props.store(fw, "");
            //props.list(System.out);
            fw.close();
            in.close();
            return "SUCCESS";
        } catch (IOException e) {
            // TODO Auto-generated catch block
            return e.getMessage();
        }
    }

    /**
     * @param strPath
     * @return 所有配置文件名 可能无用？？？
     */
    public static List<String> getFileList(String strPath) {
        if (null == strPath) {
            strPath = System.getProperty("user.dir")+ "/config/";
        }
        File dir = new File(System.getProperty("user.dir")+ "/config/");
        List<String> filelist = new ArrayList<String>();
        File[] files = dir.listFiles(); // 该文件目录下文件全部放入数组
        if (files != null) {
            for (int i = 0; i < files.length; i++) {
                String fileName = files[i].getName();
                if (files[i].isDirectory()) { // 判断是文件还是文件夹
                    getFileList(files[i].getAbsolutePath()); // 获取文件绝对路径
                } else {
                    // 文件绝对路径
                    //String strFileName = files[i].getAbsolutePath();
                    // files[i]
                    filelist.add(fileName);
                }
            }
        }
        return filelist;
    }
}
